package com.vhall.component.service.logger.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vhall.component.dao.logger.OperateLogMapper;
import com.vhall.component.entity.logger.OperateLogCreateReqDTO;
import com.vhall.component.entity.logger.OperateLogEntity;
import com.vhall.component.entity.logger.vo.OperateLogPageReqVO;
import com.vhall.component.plugin.common.mybatis.LambdaQueryWrapperX;
import com.vhall.component.plugin.common.utils.StrUtils;
import com.vhall.component.service.logger.OperateLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;


/**
 * 操作日志
 *
 * @author mingming.shi
 */
@Slf4j
@Service
public class OperateLogServiceImpl implements OperateLogService {

    @Resource
    private OperateLogMapper operateLogMapper;

    @Value("${paas.apps.lite.appId}")
    public String appId;

    @Override
    @Async
    public void createOperateLog(OperateLogCreateReqDTO createReqDTO) {
        OperateLogEntity operateLogEntity = new OperateLogEntity();
        BeanUtils.copyProperties(createReqDTO, operateLogEntity);
        operateLogEntity.setJavaMethodArgs(StrUtils.maxLength(operateLogEntity.getJavaMethodArgs(), OperateLogEntity.JAVA_METHOD_ARGS_MAX_LENGTH));
        operateLogEntity.setResultData(StrUtils.maxLength(operateLogEntity.getResultData(), OperateLogEntity.RESULT_MAX_LENGTH));

        operateLogEntity.setCreateTime(LocalDateTime.now());
        operateLogEntity.setUpdateTime(LocalDateTime.now());
        operateLogEntity.setCreator(operateLogEntity.getUserId().toString());
        operateLogEntity.setUpdater(operateLogEntity.getUserId().toString());
        operateLogEntity.setDeleted(false);
        operateLogEntity.setTenantId(appId);

        operateLogMapper.insert(operateLogEntity);

    }

    @Override
    public Page<OperateLogEntity> getOperateLogPage(OperateLogPageReqVO reqVO) {
        // 处理基于用户昵称的查询
        Collection<Long> userIds = null;

        LambdaQueryWrapperX<OperateLogEntity> query = new LambdaQueryWrapperX<OperateLogEntity>()
                .likeIfPresent(OperateLogEntity::getModule, StringUtils.isNotBlank(reqVO.getModule()) ? reqVO.getModule() : null)
                .inIfPresent(OperateLogEntity::getUserId, userIds)
                .eqIfPresent(OperateLogEntity::getType, reqVO.getType())
                .likeIfPresent(OperateLogEntity::getUserNickname, StringUtils.isNotBlank(reqVO.getUserNickname()) ? reqVO.getUserNickname() : null)
                .betweenIfPresent(OperateLogEntity::getStartTime, reqVO.getStartTime());
        if (Boolean.TRUE.equals(reqVO.getSuccess())) {
            query.eq(OperateLogEntity::getResultCode, 200);
        } else if (Boolean.FALSE.equals(reqVO.getSuccess())) {
            query.gt(OperateLogEntity::getResultCode, 200);
        }
        // 降序
        query.orderByDesc(OperateLogEntity::getId);

        Page<OperateLogEntity> page = new Page<>(reqVO.getPage(), reqVO.getPagesize());
        return operateLogMapper.selectPage(page, query);

    }

}
